<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>

<div id="addtrainingftladdercontent">

<h4>Add FTladder Stats</h4>

<form>

<select class="form-control"  name="season" id="select3">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

	echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT season 
FROM seasons";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<br>

<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

?>

<input class="form-control" type="date" name="date" placeholder="Date of FT-ladder" required/>

<br>
<?php

	$sql2 = "SELECT teams.playerID, users.firstname, users.lastname, teams.season, users.user_key, users.playernumber
	FROM teams 
	JOIN users 
	ON teams.playerID = users.user_key 
	WHERE teams.season = '$setseason'
	ORDER BY users.playernumber";

	//$result2 = $db->query($sql2);

	if ($result2 = $db->query($sql2)) {
		while ($row2 = $result2->fetch_assoc()) {
			$playerID = $row2['playerID'];
			$firstname = $row2['firstname'];
			$lastname = $row2['lastname'];

			echo "<div class=\"col-sm-6 col-md-6 col-lg-4\"> $firstname $lastname: </div>";
		
?>
<div class="col-sm-3 col-md-3 col-lg-3">
<select class="form-control"  name="<?= $playerID ?>">
	<option value="yes">Yes</option>
	<option value="no" selected="selected">No</option>
</select>
</div>


<br><br>

<?php

}
?>

<br>
<button type="submit" class="btn btn-success btn-block" id="trainingaddftladdersubmitbutton"> Add FT-ladder Stats</button>

<br>

<?php
	} else {
		echo "<br><center>Error: " . $sql . "<br>" . $db->error . "</center>";
	}

}
?>

</form>

</div>

<script type="text/javascript">
	
$("#select3").on("change", function(e) {
    postData = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "./training_addftladderstats.php",
        data: postData,
        success: function(data) { 
            $("#addtrainingftladdercontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#addtrainingftladdercontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#trainingaddftladdersubmitbutton").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./includes/handlers/training_addftladderstats_handler.php",
        data: postData,
        success: function(data) { 
            $("#addtrainingftladdercontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#addtrainingftladdercontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>